package com.traffic.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JsonConfig;
import net.sf.json.processors.DefaultValueProcessor;
import net.sf.json.processors.JsonValueProcessor;

public class JSONUtil {
    public static JsonConfig getJsonConfig(){
        JsonConfig config = new JsonConfig();
        config.registerDefaultValueProcessor(Date.class, new DefaultValueProcessor() {
            @SuppressWarnings("unchecked")
			public Object getDefaultValue(Class type) {
                return "";
            }
        });
        config.registerJsonValueProcessor(Date.class, new JsonValueProcessor() {
            public Object processObjectValue(String paramString,
                    Object paramObject, JsonConfig paramJsonConfig) {
                if (paramObject == null) {
                    return null;
                }
                String ret = null;
                try {
                    SimpleDateFormat format = new SimpleDateFormat(
                            "yyyy-MM-dd HH:mm:ss");
                    ret = format.format((Date) paramObject);
                } catch (Exception e) {
                    SimpleDateFormat format = new SimpleDateFormat(
                            "yyyy-MM-dd");
                    ret = format.format((Date) paramObject);
                }
                return ret;
            }
            
            @Override
            public Object processArrayValue(Object paramObject,
                    JsonConfig paramJsonConfig) {
                return null;
            }
        });
        return config;
    }
}
